﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL;
using Framework.Library.Core.Utils;
using System.Collections;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils.Text;
using Framework.Library.Core.Utils.URLBuilder;

namespace Framework.Portal.Layouts.Admin.Components.BGTT.Courses
{
    public partial class PedagogueIteraction : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public PedagogueIteraction()
        {
        }

        public PedagogueIteraction(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                cmdAdd.CommandName = "insert";
                lblCommandName.Text = MENUNAME;
                PortalUser info = LoadUser();
                if (info != null)
                {
                    lblUserID.Text = info.USERID.ToString();
                    lblName.Text = info.USERNAME;
                    LoadData(info.USERID);
                }
                else
                    GetDefault();
            }
        }
        private void LoadData(int userId)
        {
            BGTTPedagogues infoPeda = ManagerFactory.DAOEntity.GetInfo(new BGTTPedagogues() { USERID = userId });
            if (infoPeda != null)
            {
                cmdAdd.LocalizedPage = "ACTION";
                cmdAdd.LocalizedTag = "EDIT";
                cmdAdd.CommandName = "update";
                lblID.Value = infoPeda.ID.ToString();
                txtName.Text = infoPeda.TITLE;
                txtImage.Text = infoPeda.AVATAR;
                txtDesription.Value = infoPeda.DESCRIPTION;
            }
            else
            {
                cmdAdd.CommandName = "insert";
                lblID.Value = string.Empty;
                txtName.Text = string.Empty;
                txtImage.Text = string.Empty;
                txtDesription.Value = string.Empty;
            }
        }
        private PortalUser LoadUser()
        {
            int userID = ConvertUtility.ToInt32(Request.QueryString["uid"]);
            if(userID > 0)
            {
                PortalUser info = ManagerFactory.PortalUserManager.GetUser(userID);
                if (info != null)
                    return info;
            }
            return null;
        }

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            BGTTPedagogues info = GetData();  
            if (((Button)sender).CommandName == "insert")
                ManagerFactory.DAOEntity.Insert(info);
            else
            {
                info.ID = ConvertUtility.ToInt32(lblID.Value);
                ManagerFactory.DAOEntity.Update(info);
            }
            GetDefault();
        }

        protected void cmdEmpty_Click(object sender, EventArgs e)
        {
            int userID = ConvertUtility.ToInt32(Request.QueryString["uid"]);
            LoadData(userID);
        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(lblID.Value))
            {
                ManagerFactory.DAOEntity.Delete(new BGTTPedagogues() { ID = ConvertUtility.ToInt32(lblID.Value) });
                GetDefault();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GetDefault();
        }
        private void GetDefault()
        {
            var urlParser = new SimpleURLParameterParser(Request.RawUrl);
            string[] lstUrl = new string[] { "t", "uid" };
            Response.Redirect(urlParser.CreateQueryString(lstUrl));
        }

        private BGTTPedagogues GetData()
        {
            var info = new BGTTPedagogues();
            info.USERID = Convert.ToInt32(lblUserID.Text);
            info.NAME = lblName.Text;
            info.TITLE = txtName.Text;
            info.DESCRIPTION = StringUtils.RemoveHtml(txtDesription.Value);
            info.AVATAR = txtImage.Text;           
            return info;
        }
    }
}